package com.ruoyi.mall.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.mall.service.IMallCustomerService;
import com.ruoyi.mall.service.IMallEmployeeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.domain.MallOrderMaster;
import com.ruoyi.mall.service.IMallOrderMasterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 * 
 * @author banana_slices
 * @date 2024-12-01
 */
@RestController
@RequestMapping("/mall/master")
public class MallOrderMasterController extends BaseController
{
    @Autowired
    private IMallOrderMasterService mallOrderMasterService;
    @Autowired
    private IMallCustomerService mallCustomerService;
    @Autowired
    private IMallEmployeeService mallEmployeeService;

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('mall:master:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallOrderMaster mallOrderMaster)
    {
        startPage();

        if (getUserId() != 1)
        {
            //若userId不为1则添加查询限制
            mallOrderMaster.setCreateBy(getUsername());
        }
        List<MallOrderMaster> list = mallOrderMasterService.selectMallOrderMasterList(mallOrderMaster);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('mall:master:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallOrderMaster mallOrderMaster)
    {
        List<MallOrderMaster> list = mallOrderMasterService.selectMallOrderMasterList(mallOrderMaster);
        ExcelUtil<MallOrderMaster> util = new ExcelUtil<MallOrderMaster>(MallOrderMaster.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:master:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(mallOrderMasterService.selectMallOrderMasterByOrderId(orderId));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('mall:master:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallOrderMaster mallOrderMaster)
    {
        //验证客户,职员是否存在
        if(!mallCustomerService.checkCustomerExist(mallOrderMaster.getCustomerId())) return AjaxResult.error("客户不存在");
        if(!mallEmployeeService.checkEmployeeExist(mallOrderMaster.getEmployeeId())) return AjaxResult.error("职员不存在");

        mallOrderMaster.setCreateBy(getUsername());

        return toAjax(mallOrderMasterService.insertMallOrderMaster(mallOrderMaster));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('mall:master:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallOrderMaster mallOrderMaster)
    {
        return toAjax(mallOrderMasterService.updateMallOrderMaster(mallOrderMaster));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('mall:master:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(mallOrderMasterService.deleteMallOrderMasterByOrderIds(orderIds));
    }
}
